package com.example.until;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 自动创建实体类
 * @author Administrator
 */
public class AutoCreateEntity {
	
	private static final Logger logger = LoggerFactory.getLogger(AutoCreateEntity.class);
	private String entityName;
	
	private String entityPath = "src/main/java/com/example/demo/entity/";
	
	private String suffix = ".java";
	
	protected File fileObj;
	
	private String classSource = "package com.example.demo.entity;\r\n public class ${name} {\r\n}";
	
	protected String filePath = "";
	
	public String getEntityName() {
		return entityName;
	}

	public void setEntityName(String entityName) {
		this.entityName = entityName;
	}

	public String getEntityPath() {
		return entityPath;
	}

	public void setEntityPath(String entityPath) {
		this.entityPath = entityPath;
	}

	public AutoCreateEntity (String entityName)
	{
		this.entityName = entityName + "Entity";
		
		String file = this.entityPath + this.entityName + this.suffix;
		
		this.filePath = file;
		
		File createFileObj = new File(file);
		
		this.fileObj = createFileObj;
	}
	
	/**
	 * 创建文件
	 * @return boolean
	 */
	public boolean isCreateSuccess ()
	{
		if (this.fileExits()) {
			return true;
		}
		boolean isSuccess = false;
		
		try {
			isSuccess = this.fileObj.createNewFile();
			
			FileWriter writer = new FileWriter(this.filePath);
			
			String classRouse = this.classSource.replace("${name}", this.entityName);
			System.out.println("-------123----------");
			writer.write(classRouse);
			
			writer.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		
		}
		
		
		return isSuccess;
	}
	
	/**
	 * 文件是否存在
	 */
	public boolean fileExits()
	{
		if (this.fileObj.exists()) {
			return true;
		}
		return false;
	}
	
	
}
